#include <QBoxLayout>
#include <QButtonGroup>
#include <QPainter>

#include "sidebutton.h"

#include "sidebuttonpanel.h"

SideButtonPanel::SideButtonPanel(Direction direction, QWidget *parent) :
    QWidget(parent),
    m_buttonGroup(new QButtonGroup(this)),
    m_direction(direction)
{
    //Set properties.
    setAutoFillBackground(true);
    //Set the layout.
    auto layoutDirection = (m_direction == North) ?
                QBoxLayout::LeftToRight : QBoxLayout::TopToBottom;
    //Create layouts.
    QBoxLayout *mainLayout = new QBoxLayout(layoutDirection, this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);
    setLayout(mainLayout);
    //Check the layout size.
    if(m_direction == North)
    {
        //Add spacing to left.
        mainLayout->addSpacing(SideButtonHeight);
        setFixedHeight(SideButtonHeight);
    }
    else
    {
        setFixedWidth(SideButtonHeight);
    }
    //Add left and right layout.
    m_leftLayout = new QBoxLayout(layoutDirection);
    m_rightLayout = new QBoxLayout(layoutDirection);
    mainLayout->addLayout(m_leftLayout);
    mainLayout->addStretch();
    mainLayout->addLayout(m_rightLayout);
}

void SideButtonPanel::addButton(bool isLeft, QAbstractButton *button)
{
    //Add button to button group.
    m_buttonGroup->addButton(button);
    //Add widget to layout
    if(isLeft)
    {
        //Add to left layout.
        m_leftLayout->addWidget(button);
    }
    else
    {
        //Add to right layout.
        m_rightLayout->addWidget(button);
    }
}

void SideButtonPanel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    //Draw the border.
    QPainter painter(this);
    painter.setPen(QColor(209, 209, 209));
    // Based on the direction, draw the splitter line.
    switch(m_direction)
    {
    case North:
        painter.drawLine(0, 0, width(), 0);
        break;
    case West:
        painter.drawLine(width() - 1, 0, width() - 1, height());
        break;
    case East:
        painter.drawLine(0, 0, 0, height());
        break;
    }
}

SideButtonPanel::Direction SideButtonPanel::direction() const
{
    return m_direction;
}
